package qnb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import qnb._common.easyui.EasyuiGridData;
import qnb._common.easyui.EasyuiPageVO;
import qnb._common.tools.StringTool;
import qnb._common.vo.MessageVOFactory;
import qnb.entity.Course1;
import qnb.entity.Course2;
import qnb.entity.Grade;
import qnb.service.Course12Service;
import qnb.service.GradeService;
import weixinkeji.vip.expand.poi.JWEOfficeWebTool;
import weixinkeji.vip.jweb.power.ann.JWPIdentifiter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;


@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private GradeService gradeService;

    @Autowired
    private Course12Service course12Service;


    @JWPIdentifiter("course:add")
    @RequestMapping("/forward/courseAdd")
    public ModelAndView forwardTAdd(ModelAndView modelAndView) {
        modelAndView.setViewName("course/courseAdd");
        return modelAndView;
    }

    @JWPIdentifiter("course:select")
    @RequestMapping("/forward/courseList")
    public ModelAndView forwardList(ModelAndView modelAndView) {
        modelAndView.setViewName("course/courseList");
        return modelAndView;
    }


    @JWPIdentifiter("course:update")
    //跳转到  修改视图
    @RequestMapping("/forward/update")
    public String forwardToUpdate(Integer id, HttpServletRequest req) {
        if (null == id || id < 0) {
            return null;
        }

        Course1 obj=course12Service.select1ById(id);
        if(null==obj||null==obj.getCourse1Id()) {
            return null;
        }

        req.setAttribute("Course1Object",obj);
        return "course/update";
    }

//    //===================================== 上面的为跳转方法====================================


    @PostMapping("/selectAllGradeName")
    @ResponseBody
    public Object selectAllGradeName() {
        List<Grade> listGradeName = gradeService.selectAllGradeName();
        return listGradeName;
    }

    /**
     * 添加课程表表主体
     *
     * @param headObject
     * @param bodyData
     * @return
     */
    @JWPIdentifiter("course:add")
    @RequestMapping("/insertCourse")
    @ResponseBody
    public String insertCourse(Course1 headObject, String bodyData) {
        //从表单分离出：表体数据
        List<Course2> list = StringTool.jsonStringToList(Course2.class, bodyData);
        System.out.println(bodyData);
        return MessageVOFactory.getAddMessage(course12Service.insertCourse(headObject, list));
    }

    /**
     * 分页查询表头
     *
     * @param
     * @return
     */
    @RequestMapping("/selectCourse1")
    @ResponseBody
    public Object selectCourse(EasyuiPageVO vo) {
        if (vo.isSqlError()) {
            return null;
        }
        String whereSql = vo.getWhereSql();
        vo.setSort("course1_id");
        vo.setOrder("desc");

        return EasyuiGridData.getEasyuiGridData(course12Service.select1Count(whereSql),
                course12Service.select(whereSql, vo.getOrderbySQL(), vo.getPageSQL(), vo.getRows()));
    }

    // 表体：查询
    @RequestMapping("/list2")
    @ResponseBody
    public Object list2(HttpServletResponse resp ,Integer id) throws ServletException, IOException {
        if(null==id) {
            return "[]";
        }
        List<Course2> list=course12Service.select2ByCourse1Id(id);
        return EasyuiGridData.getEasyuiGridData(list.size(),list);
    }

    /**
     * 根据课程表主体的表头字段进行数据回显
     *
     * @param course1Id
     * @return
     */
    @RequestMapping("/selectCourse2Detail")
    @ResponseBody
    public Object selectCourse2Detail(Integer course1Id) {
        if (null == course1Id || course1Id < 0) {
            return null;
        }
        return course12Service.select2ByCourse1Id(course1Id);
    }

    /**
     * 删除课程表数据
     *
     * @param
     * @return
     */
    @JWPIdentifiter("course:dell")
    @RequestMapping("/deleteCourse")
    @ResponseBody
    public String deleteCourse(Integer course1Id) {
        if (null == course1Id || course1Id < 0) {
            return null;
        }
        return MessageVOFactory.getDellMessage(course12Service.dellCourse(course1Id));
    }

    /**
     * 修改表体数据
     *
     * @param
     * @return
     */
    @JWPIdentifiter("course:update")
    @PostMapping("/updateCourse")
    @ResponseBody
    public String updateCourse(Course1 headObject, String bodyJsonData) {

        System.out.println();
        //从表单分离出：表体数据
        List<Course2> list = StringTool.jsonStringToList(Course2.class, bodyJsonData);
        return MessageVOFactory.getUpdateMessage(course12Service.updateCourse(headObject, list));
    }

    // 表体：查询与下载
    @JWPIdentifiter("course:down")
    @RequestMapping("list/down")
    public void dow(HttpServletResponse resp, Integer id) throws ServletException, IOException {
        Course1 obj = course12Service.select1ById(id);
        if (null == obj || null == obj.getCourse1Id()) {
            return;
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        List<Course2> list = course12Service.select2ByCourse1Id(id);
        JWEOfficeWebTool.downloadExcelFile(resp, list, obj.getGradeName() + "课程表(" + sf.format(obj.getCourse1Start()) + "到" + sf.format(obj.getCourse1End()) + ").xls", "课程", true);
    }
}